package com.tepth.remote.core.utils.location;

import android.content.Context;
import android.location.Criteria;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.provider.Settings;

import com.tepth.remote.core.app.Control;

/**
 * Description:定位工具类
 *
 * @author Hequn.Lee
 * @date 2018/11/12
 */

public class LocationUtil {

    /**
     * 判断是否打开了允许虚拟位置,android6.0以下及以上都可用
     *
     * @return boolean
     */
    public static boolean isAllowMockLocation() {
        boolean canMockPosition;
        //6.0以下
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
            canMockPosition = (Settings.Secure.getInt(Control.getApplicationContext()
                    .getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0);
        } else {
            try {
                //获得LocationManager引用
                LocationManager locationManager = (LocationManager) Control.getApplicationContext()
                        .getSystemService(Context.LOCATION_SERVICE);
                String providerStr = LocationManager.GPS_PROVIDER;
                LocationProvider provider = locationManager.getProvider(providerStr);
                if (provider != null) {
                    locationManager.addTestProvider(
                            provider.getName()
                            , provider.requiresNetwork()
                            , provider.requiresSatellite()
                            , provider.requiresCell()
                            , provider.hasMonetaryCost()
                            , provider.supportsAltitude()
                            , provider.supportsSpeed()
                            , provider.supportsBearing()
                            , provider.getPowerRequirement()
                            , provider.getAccuracy());
                } else {
                    locationManager.addTestProvider(
                            providerStr,
                            true,
                            true,
                            false,
                            false,
                            true,
                            true,
                            true,
                            Criteria.POWER_HIGH,
                            Criteria.ACCURACY_FINE);
                }
                locationManager.setTestProviderEnabled(providerStr, true);
                locationManager.setTestProviderStatus(providerStr, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
                // 模拟位置可用
                canMockPosition = true;
            } catch (SecurityException e) {
                e.printStackTrace();
                canMockPosition = false;
            }
        }
        return canMockPosition;
    }
}
